package com.zhongli.devplatform.security;

import com.alibaba.fastjson.JSON;
import com.zhongli.devplatform.bean.Res;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.Serializable;

public class JwtAuthenticationEntryPoint implements AuthenticationEntryPoint, Serializable {

    private static final long serialVersionUID = -7858869558953243875L;

    @Override
    public void commence(HttpServletRequest request, HttpServletResponse response,
                         AuthenticationException authException) throws IOException {
        PrintWriter out = response.getWriter();
        out.write(JSON.toJSONString(new Res(HttpServletResponse.SC_UNAUTHORIZED, "Unauthorized")));
    }
}
